package org.orion.engine;

import javax.media.opengl.GL;

public class FrameBuffer {
	private int id;
	private int width;
	private int height;
	
	public FrameBuffer(int width, int height){
		this.width = width;
		this.height = height;
		
		int[] ids = new int[1];
		GLStatic.gl.glGenFramebuffersEXT(1, ids, 0);
		this.id = ids[0];
		
		
	}
	
	public void glBind(){
		GLStatic.gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, id);
	}
	
	public static void glUnBind(){
		GLStatic.gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, 0);
	}
	
	public void glAttach(int attachment, RenderBuffer renderBuffer){
		GLStatic.gl.glFramebufferRenderbufferEXT(GL.GL_FRAMEBUFFER_EXT, attachment, 
				GL.GL_RENDERBUFFER_EXT, renderBuffer.getId());
	}
	
	public void glAttach(int attachment, Texture texture, int level){
		GLStatic.gl.glFramebufferTexture2DEXT(GL.GL_FRAMEBUFFER_EXT, attachment, 
				texture.getType(), texture.getId(), level);
	}

	public int getHeight() {
		return height;
	}

	public int getWidth() {
		return width;
	}
	
	public boolean checkStatus(){
		int status = GLStatic.gl.glCheckFramebufferStatusEXT( GL.GL_FRAMEBUFFER_EXT );

		switch( status )
		{
			case GL.GL_FRAMEBUFFER_COMPLETE_EXT:
				//System.out.println("Success create FBO id="+id);
				return true;
			case GL.GL_FRAMEBUFFER_UNSUPPORTED_EXT:
				System.out.println("Created FBO unsuported id="+id);
				break;
			default :
				System.out.println("Unknow error FBO id="+id+" "+status);	
		}
		
		return false;
	}
}
